<?php
  $path = drupal_get_path('module', 'service');
  drupal_add_css($path . '/css/service.css');
  drupal_add_js($path . '/js/service.js');

function serviceshome() {
global $language,$base_url,$user;
 $url = $base_url . '/' . drupal_get_path('module', 'service');
 if($language->language=='ta'){
	$lang= '/ta';
}else{
  $lang='';
}
 $output = '<div class="rsslogo">' . get_rss_feed("service_rss_feed") . '</div>	';
$output.='<div class="view-header" id="viewmod_service_registration_'.$language->language.'">
		<div class="inner_tab_content">
			<div class = "inner_li"><a class="view_head_sel active" href="'.$base_url.$lang.'/service/deptname">'.t('Department wise').'</a></div>
		    <div class = "inner_li"><a class="view_head_nonsel" href="'.$base_url.$lang.'/service/atoz">'.t('Service wise').'</a></div>
			<div class = "inner_li"><a class="view_head_nonsel" href="'.$base_url.$lang.'/service/category">'.t('Category wise').'</a></div>
		    <div class = "inner_li"><a class="view_head_nonsel" href="'.$base_url.$lang.'/service/search">'.t('Search').'</a></div>
		</div>';

	my_db_change(1);

	$result = db_query("SELECT * FROM depart_mst WHERE dep_id IN
						(SELECT distinct field_src_dept_value::int as deptname from field_data_field_src_dept as a,node as n WHERE n.status = 1 AND
						(n.language='".$language->language."' OR n.language = '') AND n.nid = a.entity_id and n.vid = a.revision_id  )  ORDER BY dep_id");

	my_db_change(0);
	$output .= '<div class= "result_inner">';
	/*foreach($result as $row) {
		$output .= '<div class="clm ">
					<img  src= "'.$url.'/images/icon_main.png" >
					<a href="'.$base_url.'/service/dept/'.$row->dep_id.'">'.t($row->dept_name).'</a>
					</div>';
	}*/

	 $output .= theme('service_dept_theme',array('items' => $result));
	 $output .='</div><div class = "clear"></div></div>';
	 return $output;


}
function servicewisehome($alpha) {
$alpha=arg(2);
global $language,$base_url,$user;
 $url = $base_url . '/' . drupal_get_path('module', 'service');
 if($language->language=='ta'){
	$lang= '/ta';
}else{
  $lang='';
}
$output='<div class="view-header" id="viewmod_service_registration_'.$language->language.'">
		<div class="inner_tab_content">
			<div class = "inner_li"><a class="view_head_nonsel " href="'.$base_url.$lang.'/service/deptname">'.t('Department wise').'</a></div>
		    <div class = "inner_li"><a class="view_head_sel active" href="'.$base_url.$lang.'/service/atoz">'.t('Service wise').'</a></div>
			<div class = "inner_li"><a class="view_head_nonsel" href="'.$base_url.$lang.'/service/category">'.t('Category wise').'</a></div>
		    <div class = "inner_li"><a class="view_head_nonsel" href="'.$base_url.$lang.'/service/search">'.t('Search').'</a></div>
		</div>';

		$val= strtoupper($alpha);

		my_db_change(1);
		if($val != 'ALL'){

			$query = new EntityFieldQuery();
			$query->entityCondition('entity_type', 'node')
			  ->entityCondition('bundle', 'service_registration')
			  ->propertyCondition('status',1)
			  ->propertyCondition('title',$val.'%','LIKE')
			  //->propertyCondition('language', $language->language, '=')
			  ->propertyOrderBy('title','ASC')
			  ->pager(15,0);
		 }
		 else {

			$query = new EntityFieldQuery();
			$query->entityCondition('entity_type', 'node')
			  ->entityCondition('bundle', 'service_registration')
			  ->propertyCondition('status',1)
			  ->propertyCondition('language', $language->language, '=')
			  ->propertyOrderBy('title','ASC')
			  ->pager(15,0); 		 }
		$result = $query->execute();



        $news_items = array();
		if (isset($result['node'])) {
		  $news_items_nids = array_keys($result['node']);
          $nid = current($result['node'])->nid;
		  $news_items = entity_load('node', $news_items_nids);
		}
		// $nodes = node_load_multiple(array_keys($result['node']));
  			//print render(node_view_multiple($nodes, 'teaser')) . theme('pager');
		//print "<pre>";print_r($news_items);print "</pre>";
 		my_db_change(0);
		$output .= '<div class="result_inner">';
		$output .= get_ATOZ($alpha);
		 $i=0;
 		foreach($news_items as $row){
		 $output .= '<div class="res_title_ch">
					<p>
						<a href="'.$base_url.$lang.'/service/dept/'.$row->nid.'/'.$row->field_src_org['und'][0]['value'].'">'.$row->title.'</a>
					</p>
					</div>';
					$i++;
		}
		if($i == '0'){
			 $output .='<div class="no_record">'.t('No Results Found').' </div>';
			}

		$output .= theme('pager');
		$output .='</div>';
		$output .='<div class = "clear"></div></div>';
		return  $output;

}
function servicecategoryhome() {
global $language,$base_url,$user;
 $url = $base_url . '/' . drupal_get_path('module', 'service');
 if($language->language=='ta'){
	$lang= '/ta';
}else{
  $lang='';
}
$output='<div class="view-header" id="viewmod_service_registration_'.$language->language.'">
		<div class="inner_tab_content">
			<div class = "inner_li"><a class="view_head_nonsel " href="'.$base_url.$lang.'/service/deptname">'.t('Department wise').'</a></div>
		    <div class = "inner_li"><a class="view_head_nonsel " href="'.$base_url.$lang.'/service/atoz">'.t('Service wise').'</a></div>
			<div class = "inner_li"><a class="view_head_sel active" href="'.$base_url.$lang.'/service/category">'.t('Category wise').'</a></div>
		    <div class = "inner_li"><a class="view_head_nonsel" href="'.$base_url.$lang.'/service/search">'.t('Search').'</a></div>
		</div>';


		my_db_change(1);
		$query=db_query("select DISTINCT  cate_id,cate_name from category_mst ORDER BY cate_name ");
 		my_db_change(0);
		$output .= '<div class="result_inner">';

 		foreach($query as $row){

		 $output .= '<div class="clm ">
						<img  src= "'.$url.'/images/icon_main.png" >
						<a href="'.$base_url.$lang.'/service/category/'.$row->cate_id.'">'.t($row->cate_name).'</a>
					</div>';	
				}		
	    $output .='</div>';			
		$output .='<div class = "clear"></div></div>';		
		return $output;
}
function servicecatewisehome($catid){
global $language,$base_url,$user;
$url = $base_url . '/' . drupal_get_path('module', 'service');
$catid = arg(2);
 if($language->language=='ta'){
	$lang= '/ta';
}else{
  $lang='';
}
$output='<div class="view-header" id="viewmod_service_registration_'.$language->language.'">
		<div class="inner_tab_content">
			<div class = "inner_li"><a class="view_head_nonsel " href="'.$base_url.$lang.'/service/deptname">'.t('Department wise').'</a></div>
		    <div class = "inner_li"><a class="view_head_nonsel " href="'.$base_url.$lang.'/service/atoz">'.t('Service wise').'</a></div>
			<div class = "inner_li"><a class="view_head_sel active" href="'.$base_url.$lang.'/service/category">'.t('Category wise').'</a></div>
		    <div class = "inner_li"><a class="view_head_nonsel" href="'.$base_url.$lang.'/service/search">'.t('Search').'</a></div>
		</div>';
		my_db_change(1);
		$query = new EntityFieldQuery();
			$query->entityCondition('entity_type', 'node')
			  ->entityCondition('bundle', 'service_registration')
              ->propertyCondition('status',1)
			  //->propertyCondition('language', $language->language, '=')
			  ->fieldCondition('field_src_cate', 'value', $catid, '=')
			  ->pager(25,0);
		$result = $query->execute();



        $nid = '';
        $news_items = array();

		if (isset($result['node'])) {
		  $news_items_nids = array_keys($result['node']);
          $nid = current($result['node']);
		  $news_items = entity_load('node', $news_items_nids);
		}
		//print "<pre>";print_r($news_items);print "</pre>";
		my_db_change(0);
		$output .= '<div class= "result_inner">';

		 $output .= theme('service_cat_theme',array('items' => $news_items,'catid'=>$catid,'nid'=>$nid));
		 $output .= theme('pager');
		 $output .='</div><div class = "clear"></div></div>';
		 return $output;

}
function service_form($form_state) {
global $language,$base_url,$user;
$keyword = trim(arg(2));
 $url = $base_url . '/' . drupal_get_path('module', 'service');
 $output1 = '';
 if($language->language=='ta'){
	$lang= '/ta';
}else{
  $lang='';
}
	$output1 = '<div class="view-header" id="viewmod_service_registration_'.$language->language.'">
		<div class="inner_tab_content">
			<div class = "inner_li"><a class="view_head_nonsel " href="'.$base_url.$lang.'/service/deptname">'.t('Department wise').'</a></div>
		    <div class = "inner_li"><a class="view_head_nonsel " href="'.$base_url.$lang.'/service/atoz">'.t('Service wise').'</a></div>
			<div class = "inner_li"><a class="view_head_nonsel " href="'.$base_url.$lang.'/service/category">'.t('Category wise').'</a></div>
		    <div class = "inner_li"><a class="view_head_sel active" href="'.$base_url.$lang.'/service/search">'.t('Search').'</a></div>
		</div>';
	$form['field2'] = array(
        '#type' => 'fieldset',
        '#title' => '',
        '#description' => $output1,
        '#tree' => TRUE,
    );
	$form[ 'description'] = array(
		'#prefix'=>'<div id="result_in_'.$keyword.'" class="result_in">'
	);
	$form['field'] = array(
        '#type' => 'fieldset',
        '#title' => '',
        '#description' => '',
        '#tree' => TRUE,
    );

	$form[ 'hidden_content' ] = array(
		'#type' => 'hidden',
		 '#default_value' => 'service',
	);
	$form[ 'serv_search' ] = array(
		'#title' => t('Keywords'),
		'#type' => 'textfield',
		'#default_value' => $keyword ? $keyword : '',
		//'#required' => true,
		'#size' => 35,
	);
	$form[ 'field1'] = array(
		'#prefix'=>'<div class="description">Ex:Service,Education,Land</div>'
	);
	$form[ 'submit' ] = array(
		'#title' => 'Submit',
		'#type' => 'submit',
		'#value' => 'Go',
		'#name' => 'search',
		//'#attributes' => array('onclick' => 'serviceData(); return false;'),
		'#prefix' => '<div class="sea_go_but">',
		'#suffix' => '</div>',);


	return $form;
}

function service_form_submit($form,&$form_state){
	global $base_url;
	$str = $form_state['values']['serv_search'];
    if (empty($str)) {
      form_set_error('', t('Please Enter the Search Keyword'));
    }
	$form_state['redirect'] = 'service/search/'.$str;
	return;

}

function search_form_result(){
  global $base_url, $language;
  $dep = trim(arg(2));
  $output = '';
  if($language->language=='ta'){
  	$lang= '/ta';
  }else{
   $lang='';
  }
  $form = drupal_get_form('service_form');
  $docheader = drupal_render($form);

  my_db_change(1);

  $query = new EntityFieldQuery();
  		$query->entityCondition('entity_type', 'node')
  		  ->entityCondition('bundle', 'service_registration')
  		  ->propertyCondition('status',1)
  		  ->propertyCondition('title','%'.$dep.'%','LIKE')
  		  //->propertyCondition('language', $lang, '=')
  		  ->propertyOrderBy('title','ASC');
  $result = $query->execute();


        $news_items = array();
		if (isset($result['node'])) {
		  $news_items_nids = array_keys($result['node']);
          $nid = current($result['node'])->nid;
		  $news_items = entity_load('node', $news_items_nids);
		}

		$count = $query->count()->execute();
		my_db_change(0);
		$i=0;
		$output .= '<div class="result_inner">';
		if($count != '0') {
				$output .='<div class="clr"><div class="record_found">'.$count.' Records Found</div></div>';
		}
		foreach($news_items as $row){
		//print "<pre>";print_r($row);print "</pre>";
			$output .= '<div class="res_title_ch"><p><a href="'.$base_url.$lang.'/service/dept/'.$row->nid.'/'.$row->field_src_org['und'][0]['value'].'">'.substr($row->title,0,255).'</a></p></div>';

			$i++;
		}
		if($i == '0'){
			 $output .='<div class="no_record">'.t('No Results Found').' </div>';
			}

		return $docheader.$output;

}


function get_ATOZ($alpha){ /* Function to display alphabets*/
global $base_url;
	$list = '<div class = "alpha_list view-header">';
	for($i =65; $i < 91; $i++){
		$class = "";
		if(chr($i) == $alpha){
			$class = " view_head_sel";
		}
		else
		{
			$class = " view_head_nonsel";
		}
		$list .= '<a class = "myclass' . $class . '"  href = "' .$base_url.'/service/atoz/'. chr($i) .'" >' . chr($i) . '</a>';//   l(chr($i), chr($i), array('attributes' => array('class' => 'myclass', 'title' => t(chr($i)), 'id' => chr($i))));
	}
	$class = "";
	if($alpha == 'All' || $alpha == ''){
		$class = " view_head_sel";
	}
	else
		{
			$class = " view_head_nonsel";
		}
	$list .= '<a class = "myclass' . $class . '" href = "' .$base_url.'/service/atoz/All" title = "All" id = "All"> All </a>';
	$list .= '</div>';
	return $list;
}
